package com.min.common.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一结果返回
 * @author: fengt
 * @date: 2021/3/18 10:32
 **/
@Data
@Accessors(chain = true)
public class R extends HashMap<String, Object> {

	private static final long serialVersionUID = 1L;

	R() {}

	public static R error() {
		return error("server error");
	}
	
	public static R error(String msg) {
		return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
	}
	
	public static R error(int code, String msg) {
		return new R().put("code", code).put("message", msg).put("ok", code == 200).put("timestamp", System.currentTimeMillis());
	}

	public static R ok() {
		return ok(null);
	}

	public static R ok(Map<String, Object> data) {
		return ok("request is ok", data);
	}

	public static R ok(String msg, Map<String, Object> data) {
		R r = new R().put("code", 200).put("message", msg).put("ok", true).put("timestamp", System.currentTimeMillis());
		if (data != null) {
			r.putAll(data);
		}
		return r;
	}

	@Override
	public R put(String key, Object value) {
		super.put(key, value);
		return this;
	}

}
